package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Dictionary;
import com.cloudwise.gproject.entity.User;
import com.cloudwise.gproject.service.DictionaryService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * (Dictionary)表控制层
 *
 * @author makejava
 * @since 2021-03-20 20:02:45
 */
@Controller
@RequestMapping("dictionary")
public class DictionaryController {
    /**
     * 服务对象
     */
    @Resource
    private DictionaryService dictionaryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    public Dictionary selectOne(Integer id) {
        return this.dictionaryService.queryById(id);
    }

    /**
     * @return ?
     * @create by: 卫子健
     * @description: 跳转到部门管理页面dept.html?
     * @create time: 2021/3/19 19:45
     */
    @RequestMapping("toDictionary")
    public String toDept() {
        return "back/dictionary";
    }

    /**
     * @return com.cloudwise.trademark.util.Result?
     * @create by: 卫子健
     * @description: 分页条件查询所有部门列表?
     * @create time: 2021/3/19 20:35
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Dictionary dictionary) {
        System.out.println(dictionary.getDictionaryId()+"*******************************************");
        PageInfo pageInfo = dictionaryService.queryAll(page, limit, dictionary);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * @param ?
     * @return ?
     * @create by: 卫子健
     * @description: 根据id删除部门?
     * @create time: 2021/3/19 20:57
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer dictionaryId) {
        boolean b = dictionaryService.deleteById(dictionaryId);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, b);
    }

    /**
     * @create by: 卫子健
     * @description: 删除多个id对应的数据?
     * @create time: 2021/3/19 23:21
     */
    @ResponseBody
    @RequestMapping("deleteByIds")
    public Result deleteByIds(String dictionaryIds) {
        int i = dictionaryService.deleteIds(dictionaryIds);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, i);
    }

    /**
     * @create by: 卫子健
     * @description: 更新单个数据?
     * @create time: 2021/3/19 21:44
     */
    @ResponseBody
    @PutMapping("update")
    public Result update(Dictionary dictionary, HttpSession session) {
        //获取当前用户的用户名即为 执行了更新操作的人
        User userInfo = (User) session.getAttribute("userInfo");
        dictionary.setUpdateBy(userInfo.getUserName());
        Dictionary update = dictionaryService.update(dictionary);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, update);
    }

    @ResponseBody
    @PostMapping("insert")
    public Result insert(Dictionary dictionary, HttpSession session) {
        User userInfo = (User) session.getAttribute("userInfo");
        dictionary.setCreateBy(userInfo.getUserName());
        dictionary.setUpdateBy(userInfo.getUserName());
        Dictionary insert = dictionaryService.insert(dictionary);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, insert);
    }
}
